
package com.updesignsz.admin.controller;

import com.updesignsz.admin.service.NewsService;
import com.updesignsz.admin.util.ResponseUtil;
import com.updesignsz.admin.vo.form.NewsForm;
import com.updesignsz.admin.vo.form.NewsForm;
import com.updesignsz.commom.entity.News;
import com.updesignsz.commom.entity.News;
import com.updesignsz.commom.entity.News;
import com.updesignsz.commom.util.DateUtil;
import com.updesignsz.commom.util.JsonUtil;
import com.updesignsz.commom.util.JsonUtils;
import com.updesignsz.commom.util.ResultVo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 新闻
 * Creation date 2020-8-8 14:56:12
 */
@RestController
public class NewsController {

	@Autowired
	private NewsService newsService;
	
	/**
	 * 新闻列表
	 */
	@GetMapping("/news")
	public void insuranceNews(News news, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		vo.setData(newsService.listAll(news));
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}

	/**
	 * 新闻详情
	 */
	@GetMapping("/news/{newsId}")
	public void news(@PathVariable Integer newsId, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		vo.setData(newsService.getById(newsId));
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}

	/**
	 * 保存 新闻
	 */
	@PostMapping("/news")
	public void addNews(@Validated(PostMapping.class) @RequestBody NewsForm form, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		News news = convert(form);
		newsService.saveNews(news);
		vo.setData(newsService.getById(news.getId()));
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}


	/**
	 * 删除 新闻
	 */
	@DeleteMapping("/news/{newsId}")
	public void deleteNews(@PathVariable Integer newsId, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		newsService.deleteNews(newsId);
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}

	/**
	 * 修改 新闻
	 */
	@PutMapping("/news")
	public void updateNews(@Validated(PutMapping.class) @RequestBody NewsForm form, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		News news = convert(form);
		newsService.updateNews(news);
		vo.setData(newsService.getById(news.getId()));
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}

	/**
	 * 修改 新闻
	 */
	@PatchMapping("/news")
	public void updateNews(@Validated(PatchMapping.class) @RequestBody News news, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		newsService.updateNews(news);
		vo.setData(newsService.getById(news.getId()));
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}


	private News convert(NewsForm newsForm) {
		News news = new News();
		news.setId(newsForm.getId());
		news.setTitle(newsForm.getTitle());
		news.setContent(newsForm.getContent());
		news.setReleaseDate(DateUtil.getStrByDataTime(newsForm.getReleaseDate(), "yyyy-MM-dd"));
		news.setType(newsForm.getType());
		news.setCover(newsForm.getCover());
		String[] images = newsForm.getCover().split(",");
		if (images.length > 0) {
			news.setCoverThumbnail(images[0].replace("uploads/", "uploads/thumb_"));
		}
		return news;
	}

}